package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.distribution.DistributionSubstations;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.SupplierInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IDistributionSubstationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分站分销收益Controller
 *
 * @author ruoyi
 * @date 2021-03-12
 */
@RestController
@RequestMapping("/system/substations")
public class DistributionSubstationsController extends BaseController
{
    @Autowired
    private IDistributionSubstationsService distributionSubstationsService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SupplierInfoService supplierInfoService;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询分站分销收益列表
     */
   // @PreAuthorize("@ss.hasPermi('system:substations:list')")
    @GetMapping("/list")
    public TableDataInfo list(DistributionSubstations distributionSubstations)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        distributionSubstations.setUserId(userId);
        List<DistributionSubstations> list = distributionSubstationsService.selectDistributionSubstationsList(distributionSubstations);
        return getDataTable(list);
    }
    /** 根据分站的地址查询出 全部的店铺来 */
    @GetMapping("/findAll")
    public TableDataInfo findAll(SupplierInfo supplierInfo){

        String deptName = null; //那个分站
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        SysUser aById = sysUserMapper.findAById(userId);
        String s =aById.getDept().getDeptName();
        startPage();
        //根据分站的地区 查询出全部的店铺
        supplierInfo.setAddress(s);
        List<SupplierInfo> allAdder = supplierInfoService.findAllAdder(supplierInfo);
        return getDataTable(allAdder);
    }

    /** 根据分站地址查询出 全部的订单来 */

    /**
     * 导出分站分销收益列表
     */
    //@PreAuthorize("@ss.hasPermi('system:substations:export')")
    @Log(title = "分站分销收益", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DistributionSubstations distributionSubstations)
    {
        List<DistributionSubstations> list = distributionSubstationsService.selectDistributionSubstationsList(distributionSubstations);
        ExcelUtil<DistributionSubstations> util = new ExcelUtil<DistributionSubstations>(DistributionSubstations.class);
        return util.exportExcel(list, "substations");
    }

    /**
     * 获取分站分销收益详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:substations:query')")
    @GetMapping(value = "/{distriSubId}")
    public AjaxResult getInfo(@PathVariable("distriSubId") Long distriSubId)
    {
        return AjaxResult.success(distributionSubstationsService.selectDistributionSubstationsById(distriSubId));
    }

    /**
     * 新增分站分销收益
     */
    //@PreAuthorize("@ss.hasPermi('system:substations:add')")
    @Log(title = "分站分销收益", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DistributionSubstations distributionSubstations)
    {
        return toAjax(distributionSubstationsService.insertDistributionSubstations(distributionSubstations));
    }

    /**
     * 修改分站分销收益
     */
    //@PreAuthorize("@ss.hasPermi('system:substations:edit')")
    @Log(title = "分站分销收益", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DistributionSubstations distributionSubstations)
    {
        return toAjax(distributionSubstationsService.updateDistributionSubstations(distributionSubstations));
    }

    /**
     * 删除分站分销收益
     */
   // @PreAuthorize("@ss.hasPermi('system:substations:remove')")
    @Log(title = "分站分销收益", businessType = BusinessType.DELETE)
	@DeleteMapping("/{distriSubIds}")
    public AjaxResult remove(@PathVariable Long[] distriSubIds)
    {
        return toAjax(distributionSubstationsService.deleteDistributionSubstationsByIds(distriSubIds));
    }


    @GetMapping(value = "/findAllSum")
    public int findAllSum(DistributionSubstations distributionSubstations){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
//        Long userId = (long)38;
        distributionSubstations.setUserId(userId);
        int allSum = distributionSubstationsService.findAllSum(distributionSubstations);
        return allSum;
    }
}
